package com.javaziliao.design.bridge.function;

import com.javaziliao.design.pojo.UserInfo;
import com.javaziliao.design.repo.UserRepository;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public abstract class AbstractRegisterLoginFunc implements IRegisterLoginFunc {


    protected String commonLogin(String account, String password, UserRepository userRepository) {
        UserInfo userInfo = userRepository.findByUserNameAndUserPassword(account, password);
        if (userInfo != null) {
            return "登录成功";
        }
        return "账号或密码错误";
    }

    protected String commonRegister(UserInfo userInfo, UserRepository userRepository) {
        if (commonCheckUserExists(userInfo.getUserName(), userRepository)) {
            throw new RuntimeException("用户已存在");
        }
        userInfo.setCreateDate(new Date());
        userRepository.save(userInfo);
        return "注册成功";
    }

    protected boolean commonCheckUserExists(String userName, UserRepository userRepository) {
        UserInfo userInfo = userRepository.findByUserName(userName);
        return userInfo != null;
    }


    @Override
    public String register(UserInfo userInfo) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String login(String account, String password) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean checkUserExists(String userName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String login3rd(HttpServletRequest request) {
        throw new UnsupportedOperationException();
    }
}
